package org.liuyunfeng.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: TanJingyu
 * @create: 2022-06-21 15:18
 **/
@RestController
@RequestMapping("/lock")
public class LockController {
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    @GetMapping
    public Object lock() {
        int i = 0;
        try {
            lock.lock();
            condition.await();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            i++;
            System.out.println(i);
            lock.unlock();
        }

        return i;
    }

    @GetMapping("/signal")
    public void singal() {
        lock.lock();
        condition.signalAll();
        lock.unlock();
    }
}

    